package com.btex.app.pager;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.btex.app.R;
import com.btex.app.SeaechActivity;
import com.btex.app.adapter.CoinsAdapter;
import com.btex.app.adapter.MyFragmentAdapter;
import com.btex.app.tools.CookieTool;
import com.btex.app.tools.SharedPreferencesUtil;
import com.lidroid.xutils.util.PreferencesCookieStore;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhangyi<zhangyi, 1104745049@qq.com>
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/5/18
 * @time 上午11:41
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */
public class FragmentHangQing extends BaseFragment {
    private static final String TAG = "FragmentHangQing";
    @BindView(R.id.activity_hangqing_tab)
    TabLayout mTabLayout;
    @BindView(R.id.fragent_hangqing_view_pager)
    ViewPager mFragentHangqingViewPager;
    @BindView(R.id.activity_lin_zhang_die)
    LinearLayout mActivityLinZhangDie;
    @BindView(R.id.activity_lin_cheng_jiao)
    LinearLayout mActivityLinChengJiao;
    @BindView(R.id.activity_imag_zhang_die)
    ImageView mActivityImagZhangDie;
    @BindView(R.id.activity_imag_cheng_jiao)
    ImageView mActivityImagChengJiao;
    @BindView(R.id.activity_hang_qing_rela)
    RelativeLayout mActivityHangQingRela;

    private CoinsAdapter coinsAdapter;
    private PreferencesCookieStore cookieStore;
    private List<Fragment> mFragments;
    private SharedPreferencesUtil sp;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        cxt = getActivity();
        view = inflater.inflate(R.layout.activity_list_refresh_nobar, null);
        ButterKnife.bind(this, view);
        sp = SharedPreferencesUtil.getSharedPreferences(getActivity());
        initFragment();
        initTab();
        coinsAdapter = new CoinsAdapter(cxt);
        cookieStore = CookieTool.getCookieStore(cxt, sp);
        setData();

        return view;
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(FirstFragment.newInstance());
        mFragments.add(UsdtFragment.newInstance());
        mFragments.add(EthFragment.newInstance());
        mFragments.add(BtcFragment.newInstance());
//        mFragments.add(DogeFragment.newInstance());


    }

    private void initTab() {

        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            strings.add(getString(R.string.zi_xuan));
            strings.add("USDT");
            strings.add("ETH");
            strings.add("BTC");
//            strings.add("DOGE");


        }
        mTabLayout.addTab(mTabLayout.newTab().setText(strings.get(0)));
        mTabLayout.addTab(mTabLayout.newTab().setText(strings.get(1)));
        mTabLayout.addTab(mTabLayout.newTab().setText(strings.get(2)));
        mTabLayout.addTab(mTabLayout.newTab().setText(strings.get(3)));
//        mTabLayout.addTab(mTabLayout.newTab().setText(strings.get(4)));
        mTabLayout.setupWithViewPager(mFragentHangqingViewPager);

        mFragentHangqingViewPager.setOffscreenPageLimit(4);
        mFragentHangqingViewPager.setAdapter(new MyFragmentAdapter(getActivity().getSupportFragmentManager(), mFragments, strings));
        mFragentHangqingViewPager.setCurrentItem(1);


    }


    @Override
    public void setData() {


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }


    @OnClick({R.id.activity_lin_zhang_die, R.id.activity_lin_cheng_jiao, R.id.activity_hang_qing_rela})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_lin_zhang_die:
                setImagBakground();
                mActivityImagZhangDie.setBackground(getResources().getDrawable(R.mipmap.yi_xuan));
                EventBus.getDefault().post("zhangdie");
                sp.putBoolean("ischengjiao", false);

                break;
            case R.id.activity_lin_cheng_jiao:
                setImagBakground();
                mActivityImagChengJiao.setBackground(getResources().getDrawable(R.mipmap.yi_xuan));
                EventBus.getDefault().post("chengjiao");
                sp.putBoolean("ischengjiao", true);
                break;
            case R.id.activity_hang_qing_rela:

                Intent intent = new Intent(getActivity(), SeaechActivity.class);
                startActivity(intent);

                break;
        }
    }

    void setImagBakground() {
        mActivityImagZhangDie.setBackground(getResources().getDrawable(R.mipmap.xuanzhe));
        mActivityImagChengJiao.setBackground(getResources().getDrawable(R.mipmap.xuanzhe));
    }
}
